﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using System.Diagnostics;
using Microsoft.Xna.Framework.Graphics;

namespace firstrpg
{

    public enum turftype
    {
        none = 0,
        waypoint = 1,
        area = 2,

        //waypoints and areas are invisible, and allow other tiles to be stacked upon it.

        grass = 3,
        wall = 4
    }

    public class turfs
    {

        public turftype type = turftype.none;
        public int x;
        public int y;
        public double timeCreated;

    }




    //handles all management of turfs/the world
    public class turfManager : List<turfs>
    {

        //somethings for loading
        public int percent = 0;    //the percent of tiles loaded upon init tiles.

        //the map dimensions.
        int maxx = 5000;
        int maxy = 6000;

        public turfs[,] contents;
        public turfs[,] active = new turfs[100,100];

        public turfManager()
        {
            contents = new turfs[maxx, maxy];
        }


        
        
        
        /// <summary>
        /// Returns a list of tiles within a rectangle.
        /// <para>Topleft is x,y - bottom right is width/height</para>
        /// </summary>
        /// <param name="r"></param>
        /// <param name="width"></param>
        /// <param name="height"></param>
        /// <returns></returns>
        public turfs[] returnRange(Rectangle r, float width = 32f, float height = 32f)   //takes a rectangle of coordinates, and returns the tiles located in those translated x.y tile coordinates
         
        {
            List<turfs> list = new List<turfs>();
            Rectangle newrec = new Rectangle();
            int left = (int)(r.X / width);
            int right = (int)((r.Width / width) + 1f);

            int top = (int)(r.Y / height);
            int bottom = (int)((r.Height / height) + 2f);

            //these variables above are the range in tiles after being transformed by whatever grid you defined (default 32,32)
            newrec.X = left;
            newrec.Y = top;

            newrec.Width = right;
            newrec.Height = bottom;



            //lets loop now
            for (int i = left; i < right; i++) //from left to right, we loop into a sub row
            {
                for (int n = top; n < bottom; n++)
                {
                    //attempt to grab the turf at this location. if its there, add to the list
                    turfs retreive_turf = GetTurf(i, n);
                    if (retreive_turf != null)
                        list.Add(retreive_turf);

                }
            }



            turfs[] rlist = list.ToArray();
            return rlist;
        }


        /// <summary>
        /// Returns a turfs class at x/y location (*32)
        /// </summary>
        /// <param name="x"></param>
        /// <param name="y"></param>
        /// <returns></returns>
        public turfs GetTurf(int x, int y)
        {
            
            //robustness
            if (x > contents.GetLength(0))
                return null;
            if (y > contents.GetLength(1))
                return null;
            x = (int)(MathHelper.Clamp((float)x, 0f, (float)maxx - 1f));     //because the list starts at zero, maxing at 5000 will cause below to be out of bounds,
            y = (int)(MathHelper.Clamp((float)y, 0f, (float)maxy - 1f));         //you see, there is no 5000 max, it is in reality programming wise 49999

            turfs returnTurf = null;
            if (contents[x, y] != null)
                returnTurf = contents[x, y];
            else    //it is null, but its within range.
            {
                turfs toAdd = new turfs();
                toAdd.x = x;
                toAdd.y = y;
                toAdd.timeCreated = client.seconds;

                contents[x, y] = toAdd;
                if (x < 100 && y < 100)
                    active[x, y] = toAdd;
                

            }

            return returnTurf;
        }



        //unused
        void InittTiles()    //initialize tiles/ set up the map.
        {
            percent = 0;
            uint num = 0;
            for (int low = 0; low < maxx; low++)
            {
                for (int high = 0; high < maxy; high++)
                {
                    //turfs testT = null;// GetTurf(low, high);
                    // if (testT == null)
                    //nothing here, lets initialize it.
                    //{
                    num++;
                    percent = (int)(((num * 100) / (maxx * (maxy + 0.1f))));
                    if ((num % 500000) == 0)
                        Debug.WriteLine(percent);

                    if (contents[low, high] == null)
                        contents[low, high] = new turfs();
                    contents[low, high].type = turftype.grass;    //we want it to be set as grass.
                    contents[low, high].x = low;
                    contents[low, high].y = high;

                    //}



                }
            }

        }





    }
}
